package cn.edu.abtu.controller;

import cn.edu.abtu.entity.SysPermission;
import cn.edu.abtu.service.SysPermissionService;
import cn.edu.abtu.utils.AjaxResult;
import cn.edu.abtu.vo.query.PermissionQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sysPermission")
public class SysPermissionController {

    /**
     * 服务对象
     */
    @Resource
    private SysPermissionService sysPermissionService;

    @GetMapping("/list")
    public AjaxResult getPermissionList(PermissionQueryVo permissionQueryVo) {
        List<SysPermission> permissionList = sysPermissionService.getPermissionList(permissionQueryVo);
        return AjaxResult.success("查询成功!", permissionList);
    }

    @GetMapping("/parent/list")
    public AjaxResult getParentPermissionList() {
        List<SysPermission> permissionList = sysPermissionService.getParentPermissionList();
        return AjaxResult.success("查询成功!", permissionList);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysPermission sysPermission) {
        boolean result = sysPermissionService.save(sysPermission);
        if (result) {
            return AjaxResult.success("菜单添加成功!", null);
        }
        return AjaxResult.success("菜单添加失败!", null);
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody SysPermission sysPermission) {
        boolean result = sysPermissionService.updateById(sysPermission);
        if (result) {
            return AjaxResult.success("菜单修改成功!", null);
        }
        return AjaxResult.success("菜单修改失败!", null);
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        return sysPermissionService.deleteById(id);
    }
}

